package cn.t.repository.impl.user;

import cn.t.converter.user.RoleFirmConverter;
import cn.t.core.mode.YesNoEnum;
import cn.t.core.repoistory.RepositoryImpl;
import cn.t.core.enums.base.BaseStatusEnum;
import cn.t.model.user.RoleFirmDO;
import cn.t.persistence.entity.user.RoleFirmPO;
import cn.t.persistence.mapper.user.RoleFirmMapper;
import cn.t.repository.user.RoleFirmRepository;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 部门信息表数据仓库
 * @author l
 */
@Slf4j
@Repository
@AllArgsConstructor
public class RoleFirmRepositoryImpl extends RepositoryImpl<RoleFirmMapper, RoleFirmPO, RoleFirmConverter, RoleFirmDO> implements RoleFirmRepository {


    @Override
    public List<RoleFirmDO> findByModel(RoleFirmDO roleFirmDO, int start, Integer pageSize, String field, String direction) {

        String lastSql = StringUtils.hasText(field) ? " order by " + field + " " + direction + " limit " + start + "," + pageSize : " limit " + start + "," + pageSize;

        List<RoleFirmPO> list = getWrapper(roleFirmDO).last(lastSql).list();
        if (CollectionUtils.isEmpty(list)) {
            return Collections.emptyList();
        }

        return list.stream().map(converter::po2Model).collect(Collectors.toList());
    }

    private LambdaQueryChainWrapper<RoleFirmPO> getWrapper(RoleFirmDO roleFirmDO) {
        LambdaQueryChainWrapper<RoleFirmPO> wrapper = lambdaQuery();
        if (!ObjectUtils.isEmpty(roleFirmDO.getGmtCreatedStart()) && !ObjectUtils.isEmpty(roleFirmDO.getGmtCreatedEnd())) {
            wrapper.between(RoleFirmPO::getGmtCreated, roleFirmDO.getGmtCreatedStart(), roleFirmDO.getGmtCreatedEnd());
        }
        if (!ObjectUtils.isEmpty(roleFirmDO.getGmtModifiedStart()) && !ObjectUtils.isEmpty(roleFirmDO.getGmtModifiedEnd())) {
            wrapper.between(RoleFirmPO::getGmtModified, roleFirmDO.getGmtModifiedStart(), roleFirmDO.getGmtModifiedEnd());
        }
        if (!ObjectUtils.isEmpty(roleFirmDO.getFirmId())) {
            wrapper.eq(RoleFirmPO::getFirmId, roleFirmDO.getFirmId());
        }
        if (!ObjectUtils.isEmpty(roleFirmDO.getRoleId())) {
            wrapper.eq(RoleFirmPO::getRoleId, roleFirmDO.getRoleId());
        }
        if (!ObjectUtils.isEmpty(roleFirmDO.getRoleIds())) {
            wrapper.in(RoleFirmPO::getRoleId, roleFirmDO.getRoleIds());
        }
        if (!ObjectUtils.isEmpty(roleFirmDO.getIsDeleted())) {
            wrapper.eq(RoleFirmPO::getIsDeleted, roleFirmDO.getIsDeleted());
        } else {
            wrapper.eq(RoleFirmPO::getIsDeleted, YesNoEnum.FALSE.getCode());
        }
        if (!ObjectUtils.isEmpty(roleFirmDO.getStatus())) {
            wrapper.eq(RoleFirmPO::getStatus, roleFirmDO.getStatus());
        } else {
            wrapper.eq(RoleFirmPO::getStatus, String.valueOf(BaseStatusEnum.AVAILABLE.getCode()));
        }
        return wrapper;
    }

    @Override
    public long count(RoleFirmDO roleFirmDO) {
        return getWrapper(roleFirmDO).count();
    }

    @Override
    public boolean removeById(Long id) {
        return getBaseMapper().removeById(id);
    }

    @Override
    public boolean removeByRoleId(Long roleId) {
        return getBaseMapper().removeByRoleId(roleId);
    }
}
